package net.mikoo.seals.biz.service.impl;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.mikoo.seals.biz.mapper.ApplyMapper;
import net.mikoo.seals.biz.mapper.DevicesUsedMapper;
import net.mikoo.seals.biz.service.DevicesUsedServices;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.common.model.PageModel;
import net.mikoo.seals.model.Apply;
import net.mikoo.seals.model.DevicesUsed;
import net.mikoo.seals.model.DevicesUsedCert;
import net.mikoo.seals.model.DevicesUsedQuery;
import net.mikoo.seals.model.Upfile;

@Service
public class DevicesUsedServiceImpl implements DevicesUsedServices {

	@Autowired
	private DevicesUsedMapper devicesUsedMapper;
	
	@Autowired
	private ApplyMapper applyMapper;
	
	@Override
	public PageModel<DevicesUsed> getPageByQuery(DevicesUsedQuery query) {
		if(query == null) {
			throw new ParametersException("参数错误");
		}
		List<DevicesUsed> datas = devicesUsedMapper.selectPageByQuery(query);
		Integer total = devicesUsedMapper.countPageByQuery(query);
		return new PageModel<>(total, datas);
	}

	@Override
	public List<DevicesUsed> getListByQuery(DevicesUsedQuery query) {
		if(query == null) {
			throw new ParametersException("query is null");
		}
		return devicesUsedMapper.selectListByQuery(query);
	}

	@Override
	public DevicesUsed getInfoById(Long devicesUsedId) {
		if(devicesUsedId == null) {
			throw new ParametersException("devicesUsedId is null");
		}
		DevicesUsed devicesUsed = devicesUsedMapper.selectInfoById(devicesUsedId);
		
		if(devicesUsed != null) {
			Apply apply = applyMapper.selectInfoById(devicesUsed.getApplyId());
			if(apply != null) {
				List<Upfile> voucherList = applyMapper.selectApplyfiles(devicesUsed.getApplyId());
				apply.setUpfiles(voucherList);
			}
			devicesUsed.setApply(apply);
		}
		
		return devicesUsed;
	}

	@Override
	public DevicesUsed getInfoByQuery(DevicesUsedQuery query) {
		
		DevicesUsed used = devicesUsedMapper.selectInfoByQuery(query);
		
		return used;
	}

	@Override
	public List<DevicesUsedCert> getCertsByQuery(DevicesUsedQuery query) {
		List<DevicesUsedCert> usedCerts = devicesUsedMapper.selectCertsByQuery(query);
		return usedCerts;
	}
	
	@Override
	public List<DevicesUsedCert> getCertsByDevicesActId(Long devicesActId) {
		List<DevicesUsedCert> usedCerts = devicesUsedMapper.selectCertsByDevicesActId(devicesActId);
		return usedCerts;
	}
}
